package com.liuyjy.commons.masking.masking;

import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.liuyjy.commons.masking.annotation.DataMasking;
import org.springframework.stereotype.Component;

/**
 * @Author liuyjy
 * @Date 2022/7/18 20:45
 * @Description: 拦截器
 * 当 Jackson 需要将 Java 对象转换为 JSON 字符串时，会调用 findSerializer 方法来查找是否有自定义的序列化器
 **/
@Component
public class DataMaskingIntrospector extends NopAnnotationIntrospector {

    @Override
    public Object findSerializer(Annotated am) {
        DataMasking annotation = am.getAnnotation(DataMasking.class);
        if (annotation != null) {
            return new DataMaskingSerializer(annotation.value().operation());
        }
        return null;
    }

    @Override
    public Object findDeserializer(Annotated am) {
        DataMasking annotation = am.getAnnotation(DataMasking.class);
        if (annotation != null) {
            return new DataMaskingDeserializer(annotation.value().operation());
        }
        return null;
    }
}
